unit uD3DX8;

interface

uses
  uDirect3D8;

type
  PD3DXMatrix = ^TD3DXMatrix;
  TD3DXMatrix = TD3DMatrix;

function D3DXMatrixIdentity(out mOut: TD3DXMatrix): PD3DXMatrix;
function D3DXMatrixMultiply(out mOut: TD3DXMatrix; const m1, m2: TD3DXMatrix): PD3DXMatrix;
function D3DXMatrixScaling(out mOut: TD3DXMatrix; sx, sy, sz: Single): PD3DXMatrix;
function D3DXMatrixTranslation(out mOut: TD3DXMatrix; x, y, z: Single): PD3DXMatrix;
function D3DXMatrixRotationZ(out mOut: TD3DXMatrix; angle: Single): PD3DXMatrix;
function D3DXMatrixOrthoLH(out mOut: TD3DXMatrix; w, h, zn, zf: Single): PD3DXMatrix;

implementation

function D3DXMatrixIdentity(out mOut: TD3DXMatrix): PD3DXMatrix;
begin
  FillChar(mOut, SizeOf(mOut), 0);

  mOut._11:= 1;
  mOut._22:= 1;
  mOut._33:= 1;
  mOut._44:= 1;

  Result:= @mOut;
end;

function D3DXMatrixMultiply(out mOut: TD3DXMatrix; const m1, m2: TD3DXMatrix): PD3DXMatrix;
var
  temp: TD3DXMatrix;
begin
  with temp do
  begin
    _11 := m1._11 * m2._11 + m1._12 * m2._21 + m1._13 * m2._31 + m1._14 * m2._41;
    _12 := m1._11 * m2._12 + m1._12 * m2._22 + m1._13 * m2._32 + m1._14 * m2._42;
    _13 := m1._11 * m2._13 + m1._12 * m2._23 + m1._13 * m2._33 + m1._14 * m2._43;
    _14 := m1._11 * m2._14 + m1._12 * m2._24 + m1._13 * m2._34 + m1._14 * m2._44;

    _21 := m1._21 * m2._11 + m1._22 * m2._21 + m1._23 * m2._31 + m1._24 * m2._41;
    _22 := m1._21 * m2._12 + m1._22 * m2._22 + m1._23 * m2._32 + m1._24 * m2._42;
    _23 := m1._21 * m2._13 + m1._22 * m2._23 + m1._23 * m2._33 + m1._24 * m2._43;
    _24 := m1._21 * m2._14 + m1._22 * m2._24 + m1._23 * m2._34 + m1._24 * m2._44;

    _31 := m1._31 * m2._11 + m1._32 * m2._21 + m1._33 * m2._31 + m1._34 * m2._41;
    _32 := m1._31 * m2._12 + m1._32 * m2._22 + m1._33 * m2._32 + m1._34 * m2._42;
    _33 := m1._31 * m2._13 + m1._32 * m2._23 + m1._33 * m2._33 + m1._34 * m2._43;
    _34 := m1._31 * m2._14 + m1._32 * m2._24 + m1._33 * m2._34 + m1._34 * m2._44;

    _41 := m1._41 * m2._11 + m1._42 * m2._21 + m1._43 * m2._31 + m1._44 * m2._41;
    _42 := m1._41 * m2._12 + m1._42 * m2._22 + m1._43 * m2._32 + m1._44 * m2._42;
    _43 := m1._41 * m2._13 + m1._42 * m2._23 + m1._43 * m2._33 + m1._44 * m2._43;
    _44 := m1._41 * m2._14 + m1._42 * m2._24 + m1._43 * m2._34 + m1._44 * m2._44;
  end;

  mOut := temp;
  
  Result := @mOut;
end;

function D3DXMatrixScaling(out mOut: TD3DXMatrix; sx, sy, sz: Single): PD3DXMatrix;
begin
  FillChar(mOut, SizeOf(mOut), 0);

  mOut._11:= sx;
  mOut._22:= sy;
  mOut._33:= sz;
  mOut._44:= 1;

  Result:= @mOut;
end;

function D3DXMatrixTranslation(out mOut: TD3DXMatrix; x, y, z: Single): PD3DXMatrix;
begin
  FillChar(mOut, SizeOf(mOut), 0);

  mOut._11:= 1;
  mOut._22:= 1;
  mOut._33:= 1;
  mOut._44:= 1;

  mOut._41 := x;
  mOut._42 := y;
  mOut._43 := z;

  Result:= @mOut;
end;

function D3DXMatrixRotationZ(out mOut: TD3DXMatrix; angle: Single): PD3DXMatrix;
var
  s, c: Single;
begin
  FillChar(mOut, SizeOf(mOut), 0);

  mOut._33:= 1;
  mOut._44:= 1;

  c := Cos(angle);
  s := Sin(angle);
  
  mOut._11 := c;
  mOut._12 := s;
  mOut._21 := -s;
  mOut._22 := c;
  
  Result := @mOut;
end;

function D3DXMatrixOrthoLH(out mOut: TD3DXMatrix; w, h, zn, zf: Single): PD3DXMatrix;
begin
  FillChar(mOut, SizeOf(mOut), 0);

  mOut._11:= 1;
  mOut._22:= 1;
  mOut._33:= 1;
  mOut._44:= 1;

  mOut._11 := 2 / w;
  mOut._22 := 2 / h;
  mOut._33 := 1 / (zf - zn);
  mOut._34 := - zn / (zf - zn);

  Result:= @mOut;
end;

end.

